/**
 * 
 */
package com.moji.cyg.common.condition;

import org.apache.commons.lang.math.NumberUtils;

/**
 * @author shengli.caosl 2012-2-25 下午7:02:19
 */
public class Precondition {

    private Precondition(){
    }

    public static void checkArgument(boolean expression) {
        if (!expression) {
            throw new IllegalArgumentException();
        }
    }

    public static void checkArgument(boolean expression, Object errorMessage) {
        if (!expression) {
            throw new IllegalArgumentException(String.valueOf(errorMessage));
        }
    }

    public static void checkArgument(boolean expression, Object... errorMessages) {
        if (!expression) {
            StringBuilder sb = new StringBuilder();
            for (Object errorMessage : errorMessages) {
                sb.append(String.valueOf(errorMessage));
            }
            sb.append(".");
            throw new IllegalArgumentException(sb.toString());
        }
    }

    public static void checkNotNull(Object object) {
        if (object == null) {
            throw new IllegalArgumentException();
        }
    }

    public static void checkNotNull(Object object, Object errorMessage) {
        if (object == null) {
            throw new IllegalArgumentException(String.valueOf(errorMessage));
        }
    }

    public static void checkNotNull(Object object, Object... errorMessages) {
        if (object == null) {
            StringBuilder sb = new StringBuilder();
            for (Object errorMessage : errorMessages) {
                sb.append(String.valueOf(errorMessage));
            }
            sb.append(".");
            throw new IllegalArgumentException(sb.toString());
        }
    }

    public static void checkNotBlank(String str) {
        if (str == null || str.trim().length() == 0) {
            throw new IllegalArgumentException();
        }
    }

    public static void checkNotBlank(String str, Object errorMessage) {
        if (str == null || str.trim().length() == 0) {
            throw new IllegalArgumentException(String.valueOf(errorMessage));
        }
    }

    public static void checkNotBlank(String str, Object... errorMessages) {
        if (str == null || str.trim().length() == 0) {
            StringBuilder sb = new StringBuilder();
            for (Object errorMessage : errorMessages) {
                sb.append(String.valueOf(errorMessage));
            }
            sb.append(".");
            throw new IllegalArgumentException(sb.toString());
        }
    }

    public static void checkNumber(String object) {
        if (object == null || !NumberUtils.isNumber(object)) {
            throw new IllegalArgumentException();
        }
    }

    public static void checkNumber(String object, Object... errorMessages) {
        if (object == null || !NumberUtils.isNumber(object)) {
            StringBuilder sb = new StringBuilder();
            for (Object errorMessage : errorMessages) {
                sb.append(String.valueOf(errorMessage));
            }
            sb.append(".");
            throw new IllegalArgumentException(sb.toString());
        }
    }

    public static void checkNumber(String object, Object errorMessage) {
        if (object == null || !NumberUtils.isNumber(object)) {
            throw new IllegalArgumentException(String.valueOf(errorMessage));
        }
    }

    public static void checkArrayLength(Object[] object, int size) {
        if (object.length != size) {
            throw new IllegalArgumentException();
        }
    }

    public static void checkArrayLength(Object[] object, int size, Object... errorMessages) {
        if (object.length != size) {
            StringBuilder sb = new StringBuilder();
            for (Object errorMessage : errorMessages) {
                sb.append(String.valueOf(errorMessage));
            }
            sb.append(".");
            throw new IllegalArgumentException(sb.toString());
        }
    }

    public static void checkArrayLength(Object[] object, int size, Object errorMessage) {
        if (object.length != size) {
            throw new IllegalArgumentException(String.valueOf(errorMessage));
        }
    }

    public static void checkDataValid(boolean expression) {
        if (!expression) {
            throw new IllegalArgumentException();
        }
    }

    public static void checkDataValid(boolean expression, Object... errorMessages) {
        if (!expression) {
            StringBuilder sb = new StringBuilder();
            for (Object errorMessage : errorMessages) {
                sb.append(String.valueOf(errorMessage));
            }
            sb.append(".");
            throw new IllegalArgumentException(sb.toString());
        }
    }

    public static void checkDataValid(boolean expression, Object errorMessage) {
        if (!expression) {
            throw new IllegalArgumentException(String.valueOf(errorMessage));
        }
    }

    public static void checkState(boolean expression) {
        if (!expression) {
            throw new IllegalStateException();
        }
    }

    public static void checkState(boolean expression, Object... errorMessages) {
        if (!expression) {
            StringBuilder sb = new StringBuilder();
            for (Object errorMessage : errorMessages) {
                sb.append(String.valueOf(errorMessage));
            }
            sb.append(".");
            throw new IllegalStateException(sb.toString());
        }
    }

    public static void checkState(boolean expression, Object errorMessage) {
        if (!expression) {
            throw new IllegalStateException(String.valueOf(errorMessage));
        }
    }
}
